<template>
    <a-drawer :title="title" :width="450"
              @close="handleCancel(false)"
              :visible="visible"
              :maskClosable="false"
              :bodyStyle="{ paddingBottom: '60px' }">
        <a-form-model ref="ruleForm" :model="form" :rules="rules">
            <!-- 登录账号 -->
            <a-form-model-item label="模块代码" prop="code" ref="code">
                <a-input placeholder="请设置模块代码"
                         :maxLength="20"
                         v-model="form.code" :disabled="isUpdate"/>
            </a-form-model-item>
            <a-form-model-item label="模块名称" prop="name" ref="name">
                <a-input
                        :maxLength="20"
                        placeholder="请设置模块名称" v-model="form.name"/>
            </a-form-model-item>


            <a-form-model-item label="显示顺序" prop="sort" ref="sort">
                <a-input-number placeholder="显示顺序" v-model="form.sort"/>
            </a-form-model-item>


            <a-form-model-item label="模块名称" prop="description" ref="description">
                <a-textarea placeholder="模块描述" v-model="form.description"/>
            </a-form-model-item>


        </a-form-model>
        <!-- Modal 页脚 -->
        <footer class="drawer-footer">
            <a-button type="primary" :loading="loading" @click="onSubmit">保存</a-button>
        </footer>
    </a-drawer>
</template>
<script>

    import {moduleAdd, moduleUpdate} from "../public/http";

    export default {
        props: ['record'],
        data() {
            return {
                isUpdate: false, //是否是更新模式
                loading: false, //按钮Loading
                title: '添加系统模块',
                visible: true, //modal 显示状态

                form: {
                    code: null,
                    name: null,
                    description: null,
                    sort:0,

                },
                rules: {
                    code: [
                        {
                            required: true,
                            message: '请设置模块代码',
                            trigger: 'blur'
                        },
                        {
                            max: 20,
                            message: '模块代码请设置在20个字符以内',
                            trigger: 'blur'
                        },
                        {
                            pattern: /^[a-zA-Z0-9]+$/,
                            message: '模块代码只允许填写数字或者字母',
                            trigger: 'blur'
                        }
                    ],
                    name: [
                        {
                            required: true,
                            message: '请设置模块名称',
                            trigger: 'blur'
                        },
                        {
                            max: 20,
                            message: '模块名称请设置为20个字符以内',
                            trigger: 'blur'
                        }
                    ],


                    description: {
                        max: 120,
                        message: '模块描述信息请设置在120个字符以内',
                        trigger: 'blur'
                    }


                },

            };
        },
        methods: {

            //表单提交事件
            onSubmit() {
                this.$refs.ruleForm.validate(valid => {
                    //验证成功后进行数据提交
                    if (valid) {
                        this.loading = true;
                        if (this.isUpdate) {
                            moduleUpdate(this.form.id, this.form).then(res => {
                                this.$exMod.ajaxMessage(res)
                                res.code === 0 ? this.handleCancel(true) : null;


                            })
                        } else {
                            moduleAdd(this.form).then(res => {
                                this.$exMod.ajaxMessage(res);
                                res.code === 0 ? this.handleCancel(true) : null;
                            })
                        }

                        this.loading = false;
                    }
                });
            },
            //关闭弹窗 刷新
            handleCancel(render = false) {
                this.visible = false;
                this.$emit('closeModal', {render: render});
            },
            //表单初始化
            fromVal() {
                let obj = {...this.record};
                if (obj.id) {
                    this.form = obj;
                    this.isUpdate = true;
                    this.title = '编辑模块';
                }
            },
            //数据初始化
            async init() {

                this.fromVal();
            }
        },
        created() {
            this.init();
        }
    };
</script>
